const http = require('http')
const fs = require('fs')
const path = require('path')

const app = http.createServer((req, res) => {
    // 获取请求路径
    const { pathname } = new URL(req.url, 'http://127.0.0.1')

    const filePath = path.join(__dirname, `./file${pathname}`)

    fs.readFile(filePath, (err, data) => {
        if (err) {
            res.statusCode = 500
            res.setHeader("Content-type", "text/html;charset=utf-8")
            res.end('读取文件失败')
            return
        }
        res.end(data)
    })
})

app.listen(3000, () => {
    console.log('server is running...')
})